package com.king.configuration.security.authentication;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.king.util.AjaxResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 浏览器登录成功之后的处理器
 *
 * @author liukai
 */
public class BrowserAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Autowired
    private ObjectMapper objectMapper;


    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        String content = objectMapper.writeValueAsString(AjaxResultUtil.success());
        PrintWriter writer = response.getWriter();
        writer.write(content);
        writer.close();
    }

}
